//
//  HJDNewFeatureController.m
//  weibo
//
//  Created by 胡金东 on 16/3/29.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDNewFeatureController.h"
#import "HJDNewCollectionViewCell.h"
//#import "UIView+Frame.h"

@interface HJDNewFeatureController ()

@property (nonatomic,weak) UIPageControl *pageControl;

@end

@implementation HJDNewFeatureController
static NSString *identifier = @"CollCell";


-(instancetype)init
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    //清空行距
    layout.minimumLineSpacing = 0;
    //设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//水平移动
    
    return [super initWithCollectionViewLayout:layout];
}
//使用UICollectionViewController
//1.初始化的时候设置布局参数
//2.必须collectionView要注册Cell

//self.view与self.collectionView（在collectionView中self.view代表不了self.collectionView，只能自己设置，这点比较特殊）
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //注册cell,默认就会创建这个类型的cell
    [self.collectionView registerClass:[HJDNewCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    //分页
    self.collectionView.pagingEnabled = YES;
    //没有弹性
    self.collectionView.bounces = NO;
    //下面的横线不显示
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
//    self.view.backgroundColor = [UIColor redColor];//不显示
    self.collectionView.backgroundColor = [UIColor greenColor];//显示
  
    [self setUpPageControl];
}


-(void)setUpPageControl
{
    // 添加pageController,只需要设置位置，不需要管理尺寸
    UIPageControl *control = [[UIPageControl alloc]init];
    control.numberOfPages = 4;
    control.pageIndicatorTintColor = [UIColor blackColor];
    control.currentPageIndicatorTintColor = [UIColor orangeColor];
    // 设置center
    control.center = CGPointMake(self.view.width * 0.5, self.view.height-10);
    _pageControl = control;
    [self.view addSubview:control];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{// 获取当前的偏移量，计算当前第几页
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
      // 设置页数
    _pageControl.currentPage = page;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //dequeueReusableCellWithReuseIdentifier作用
    //1.首先从缓冲池里取cell
    //2.看当前是否注册cell，如果注册cell，就会帮你创建cell
    //3.没有注册，就会报错
    
    HJDNewCollectionViewCell *collCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
   
    //拼接图片名称 3.5英寸 320 480
    //indexPath.row 是从0开始
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld",indexPath.row + 1];
//    if (screenH > 480) {
//        imageName = [NSString stringWithFormat:@"new_feature_%ld-568h",indexPath.row + 1];
//    }
    
    collCell.image = [UIImage imageNamed:imageName];
    
   [collCell setIndexPath:indexPath count:4];
    return collCell;
}

@end
